package com.example.agriapp.view.fragment;

import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.agriapp.base.BaseFragment;
import com.example.agriapp.R;
import com.example.agriapp.data.AgriDatabaseHelper;
import com.example.agriapp.model.bean.Message;
import com.example.agriapp.adapter.MessageAdapter;
import com.example.agriapp.model.bean.User;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

public class MessageFragment extends BaseFragment  {
   // private MessageAdapter mAdapter;
    private MessageAdapter messageAdapter;
    private List<Message> messageList =new ArrayList<>();
    private RecyclerView recyclerView;
    private User user;
    private User userInfo;

    public static MessageFragment newInstance() {
        return new MessageFragment();
    }
    
    @Override
    protected int getLayoutId() {
        return R.layout.fragment_messgae;
    }
    
    @Override
    protected void initView(View view) {
        recyclerView = view.findViewById(R.id.rv_messages);
        Button btnSend = view.findViewById(R.id.btn_send);
        EditText etMessage = view.findViewById(R.id.et_message);
        AgriDatabaseHelper agriDatabaseHelper = new AgriDatabaseHelper(getContext());
        messageList = agriDatabaseHelper.getAllMessages();
        // 初始化消息列表
        //initMessageList();
        user = (User) getActivity().getIntent().getSerializableExtra("USER");
        userInfo = agriDatabaseHelper.getUserInfo(user.getId());
        // 设置RecyclerView
        messageAdapter = new MessageAdapter(messageList);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerView.setAdapter(messageAdapter);

        // 发送按钮点击事件
        btnSend.setOnClickListener(v -> {
            String content = etMessage.getText().toString().trim();
            if (!content.isEmpty()) {
                boolean b = agriDatabaseHelper.sendMessage(userInfo.getId(), userInfo.getRealName(), content);
                if (b)
                etMessage.setText("");
                else
                    Toast.makeText(getActivity(), "发送失败", Toast.LENGTH_SHORT).show();
            }

            messageList = agriDatabaseHelper.getAllMessages();
            messageAdapter.updateData(messageList);
        });
    }

    private void initMessageList() {
        // 模拟初始消息数据（根据图片中的内容）
        messageList.add(new Message("1372074", "[货品消息]", "你好", "晚上8:24"));
        messageList.add(new Message("1469996", "", "好好", "下午3:12"));
        messageList.add(new Message("1473079", "", "请查收", "上午10:05"));
    }

    private void sendMessage(String content) {
        // 模拟发送消息
        String currentTime = new SimpleDateFormat("HH:mm", Locale.getDefault())
                .format(new Date());
        Message newMessage = new Message(user.getRealName(), content, currentTime);

        messageList.add(0, newMessage); // 添加到列表顶部
        messageAdapter.notifyItemInserted(0);
        recyclerView.scrollToPosition(0);
    }

    @Override
    protected void initData() {
        // 数据初始化在initView中完成
    }

    
    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }
}